package com.rds.benchmark.operator;

import com.rds.benchmark.util.MakeValue;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.commands.JedisCommands;

import java.io.IOException;

public class ListOperator extends TypeOperatorBase {

    private final static int SUBCONTENTS = 1000;

    private Object conn;

    private String key;

    private int count;

    private final String value = MakeValue.value;

    @Override
    public void init(Object conn, String key, int idx,String mode) {
        this.conn = conn;
        this.key = key;
        this.count = SUBCONTENTS >> 1;
    }

    @Override
    public Long clear() {
        return ((JedisCommands) conn).del(key);
    }

    @Override
    public void operate() throws IOException {
        if(conn instanceof JedisCommands){
            JedisCommands jedis = (JedisCommands) conn;
            for (int i = 0; i < count; i++) {
                jedis.lpush(key, value);
                jedis.rpush(key, value);
                times += 2;
            }

            for (int i = 0; i < count; i++) {
                jedis.lpop(key);
                jedis.rpop(key);
                times += 2;
            }
        }
    }

}
